package com.pan.alg.leecode;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2021/7/17
 */
public class Leecode167 {


    /**
     * 通过一前一后双指针进行判断
     * 此数组为有序数组
     * @param numbers
     * @param target
     * @return
     */
    public int[] twoSum(int[] numbers, int target) {
        int[] result = new int[2];
        int head =0;
        int tail = numbers.length-1;
        while(head<tail){
            int curr=numbers[head]+numbers[tail];
            if(curr==target){
                result[0]=head+1;
                result[1]=tail+1;
                return result;
            }else if(curr<target){
                head++;
            }else {
                tail--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        //int[] nums = new int[]{1,2,3,4,5};
        int[] nums = new int[]{2,7,11,15};
        int target=9;
        Leecode167 leecode167 = new Leecode167();
        int[] result = leecode167.twoSum(nums,6);
        System.out.println(result[0]);
        System.out.println(result[1]);
    }
}
